﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Microsoft.VisualStudio.TextTemplating;
using System.CodeDom.Compiler;
using System.Resources;
using System.Globalization;
using System.Threading;

namespace SQL_EG.uc
{
    public partial class ucSecondStep : UserControl
    {
        Parametres paremetre = null;
        EntityManager entityManager = null;
        
        ResourceManager res_man;    // declare Resource manager to access to specific cultureinfo
        CultureInfo culture;   

        public ucSecondStep(Parametres param)
        {
            this.paremetre = param;
            InitializeComponent();
            
        }
      

        private async void ucSecondStep_Load(object sender, EventArgs e)
        {
            try
            {
                    LoadLanguage();
                    ckbxTables.Items.Clear();
                    PboxLoading.Visible = true;
                    lblLoading.Visible = true;
                    entityManager = new EntityManager(paremetre.serverName, paremetre.userName, paremetre.pwd);
                  
                var result = await entityManager.GetTableListAsync(paremetre.databaseName);
                   
                        foreach (var item in result)
                        {
                            ckbxTables.Items.Add(item);
                        }

                    
                    PboxLoading.Visible = false;
                    lblLoading.Visible = false;
            } 
            catch (Exception ex)
            {
                          MessageBox.Show(ex.Message);      
            }
        }

        private void LoadLanguage()
        {
            res_man = new ResourceManager("SQL_EG.Resource.Res", typeof(ucFirstStep).Assembly);

            if (Thread.CurrentThread.CurrentCulture.Name == "fr-FR")
            {

                culture = CultureInfo.CreateSpecificCulture("fr");
                gpbxTables.Text = res_man.GetString("gpbxTables", culture);
                checkAll.Text = res_man.GetString("checkAll", culture);
                lblLoading.Text = res_man.GetString("lblLoading", culture);
                //rdbAjouterDansProjet.Text = res_man.GetString("rdbAjouterDansProjet", culture);
                rdbDestination.Text = res_man.GetString("rdbDestination", culture);
                btnGen.Text = res_man.GetString("btnGen", culture);
                lblLoadFile.Text = res_man.GetString("lblLoadFile", culture);
            }
            else
            {
                culture = CultureInfo.CreateSpecificCulture("en");
                gpbxTables.Text = res_man.GetString("gpbxTables", culture);
                checkAll.Text = res_man.GetString("checkAll", culture);
                lblLoading.Text = res_man.GetString("lblLoading", culture);
                //rdbAjouterDansProjet.Text = res_man.GetString("rdbAjouterDansProjet", culture);
                rdbDestination.Text = res_man.GetString("rdbDestination", culture);
                btnGen.Text = res_man.GetString("btnGen", culture);
                lblLoadFile.Text = res_man.GetString("lblLoadFile", culture);
            }
        }

        private void ckbxTables_SelectedIndexChanged(object sender, EventArgs e)
        {
            //try
            //{
            //     lbxColumns.Items.Clear();

            //foreach (var item in entityManager.GetPropertyList(paremetre.databaseName,ckbxTables.Text))
            //{
            //    lbxColumns.Items.Add(item.Type + " : " + item.Name);
            //}
            //}
            //catch (Exception ex)
            //{
                
            //              MessageBox.Show(ex.Message);     throw;
            //}
        }

        private void checkAll_CheckedChanged(object sender, EventArgs e)
        {

            if (checkAll.Checked)
            {
                for (int i = 0; i < ckbxTables.Items.Count; i++)
                {
                    ckbxTables.SetItemChecked(i, true);

                }
                checkAll.Text = res_man.GetString("UncheckAll",culture);
            }
            else
            {
                for (int i = 0; i < ckbxTables.Items.Count; i++)
                {
                    ckbxTables.SetItemChecked(i, false);
                }
                checkAll.Text = res_man.GetString("checkAll", culture);
            }
        }

        private void rdbDestination_CheckedChanged(object sender, EventArgs e)
        {
            btnParcourir.Enabled = rdbDestination.Checked;
        }

        private async void btnGen_Click(object sender, EventArgs e)
        {
            if (!rdbDestination.Checked || tbxPath.Text == string.Empty)
            {
                Tools.Notification.ThrowMessage(res_man.GetString("SelectFolder",culture));
                return;
            }
            pbxLoadFile.Visible = true;
            lblLoadFile.Visible = true;
            this.paremetre.path = tbxPath.Text+@"\";
            //this.paremetre.nameSpace = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
            paremetre.SelectedTable = new List<string>();

            foreach (var t in ckbxTables.CheckedItems)
            {
                paremetre.SelectedTable.Add(t.ToString());
            }

            var result = await TransformAsync();

            pbxLoadFile.Visible = false;
            lblLoadFile.Visible = false;

            Tools.Notification.ThrowMessage(res_man.GetString("GenSuccess", culture));  

            
        }

        private async Task<bool> TransformAsync()
        {
            var result = false;

            return await Task.Run(() =>
            {
                TEntityGenerator templateInstance = new TEntityGenerator();
                templateInstance.Session = new Dictionary<string, object>();
                templateInstance.Session.Add("Parametre", this.paremetre);
                templateInstance.Initialize();
                templateInstance.TransformText();

                return result;
            });

        }

        private void btnParcourir_Click(object sender, EventArgs e)
        {
            DialogResult result = folderBrowser.ShowDialog();
            if (result == DialogResult.OK)
            {
                tbxPath.Text = folderBrowser.SelectedPath.ToString();
            }
        }
    }
}
